package com.fy.oa.zc.crm;
import java.text.SimpleDateFormat;
import java.util.Date;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.fy.oa.zc.domain.FySummaryView;
import com.fy.oa.zc.manager.AFyOA_FormEventManager;
import com.fy.oa.zc.manager.FyOA_FormEventManager;
import com.soa.eis.adapter.framework.message.IMsgObject;
import com.soa.eis.adapter.framework.message.impl.MsgObject;
import com.soa.eis.adapter.framework.requester.IServiceRequester;
import com.soa.eis.adapter.framework.requester.impl.BaseServiceRequester;

import fy.mdm.dao.MaterialRequestNoTimerTask;

public class BPMPricingApproval  extends AFyOA_FormEventManager implements FyOA_FormEventManager {
	private static final Log logger = LogFactory.getLog(BPMPricingApproval.class);
	public String toString() {
		return "价格审批表";
	}
	@Override
	public String dealProcessEvent(FySummaryView view, long affairid) {
		String summaryID = String.valueOf(view.getSummaryid());
	logger.info("dealProcessEvent   summaryID:"+summaryID);
	try {
		StateReturn(summaryID,"审批中");
	} catch (Exception e) {
		e.printStackTrace();
	}
		return view.toString();
	}

	@Override
	public String dealFinishEvent(FySummaryView view, long affairid) {
		String summaryID = String.valueOf(view.getSummaryid());
		logger.info("dealFinishEvent   summaryID:"+summaryID);
		try {
			StateReturn(summaryID,"结束");
		} catch (Exception e) {
			e.printStackTrace();
		}
		return view.toString();
	}

	@Override
	public String dealStopEvent(FySummaryView view) {
		String summaryID = String.valueOf(view.getSummaryid());
		logger.info("dealStopEvent   summaryID:"+summaryID);
		try {
			
			StateReturn(summaryID,"终止");
		} catch (Exception e) {
			e.printStackTrace();
		}
		return view.toString();
	}

	@Override
	public String dealCancelEvent(FySummaryView view, long userid, String message) {
		String summaryID = String.valueOf(view.getSummaryid());
		logger.info("dealCancelEvent   summaryID:"+summaryID);
		try {
			StateReturn(summaryID,"取消");
		} catch (Exception e) {
			e.printStackTrace();
		}
		return view.toString();
	}

	@Override
	public String dealStepBackEvent(FySummaryView view) {
		String summaryID = String.valueOf(view.getSummaryid());
		logger.info("dealStepBackEvent   summaryID:"+summaryID);
		try {
			StateReturn(summaryID,"回退");
		} catch (Exception e) {
			e.printStackTrace();
		}
		return view.toString();
	}
	
	public boolean StateReturn(String  flowID,String flowState) throws Exception {
		try {
				IServiceRequester Requester = BaseServiceRequester.getInstance();
				// 定义请求报文对象
				IMsgObject reqMo = new MsgObject(IMsgObject.MOType.initSR);
				String SourceSysID = "06004"; // 请求方(发布方)
				String ServiceID = "02007000000008";// 响应方(发布方)
				// 获取系统时间
				Date date = new Date();
				SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyyMMddHHmmss");
				String format = simpleDateFormat.format(date);
				// 对请求报文对象进行设置
				reqMo.setSourceSysID(SourceSysID); // 设置报文 请求方系统号
				reqMo.setSerialNO(format.substring(0, 8) + SourceSysID
						+ String.format("%06d", ++MaterialRequestNoTimerTask.requestNo));// 设置报文流水号,第三段表示今天发送的第几的一次报文
				reqMo.setServiceID(ServiceID); // 设置报文 提供方服务号
				reqMo.setServiceDateTime(format); // 设置报文 日期时间
				reqMo.setReqValue("FLOW_ID",flowID);
				reqMo.setReqValue("FLOW_STATE",flowState);
				//System.out.println("111111111"+reqMo);
				IMsgObject respMo = null;
				respMo = Requester.execute(reqMo);
//				System.out.println("~~~~~~~~~~~~~~~" + respMo);
				logger.info(reqMo);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return true;
	}

}
